function getLocalStroage() {
  if (!window.localStorage) {
    console.warn('浏览器不支持LocalStroage!')
    return
  }
  return window.localStorage
}

/**
 * LocalStroage是否存在key
 * @param key
 * @returns {boolean}
 */
function isExist(key) {
  var ls = getLocalStroage()
  return ls && ls.length === 0 ? false : ls.getItem(key) !== null
}

/**
 * 操作localstorage
 * @type {{set: localStorage.set, get: localStorage.get, remove: localStorage.remove}}
 */
export const localStorage = {
  set: function (key, value) {
    if (arguments.length === 1 && Object.prototype.toString.call(key).slice(8, -1) === 'Object') {
      for (var name in key) {
        if (Object.prototype.toString.call(key[name]).slice(8, -1) === 'Object') key[name] = JSON.stringify(key[name])
        getLocalStroage().setItem(name, key[name])
      }
    } else {
      if (Object.prototype.toString.call(value).slice(8, -1) === 'Object') value = JSON.stringify(value)
      getLocalStroage().setItem(key, value)
    }
    return this
  },
  get: function (key) {
    if (isExist(key)) {
      var value = getLocalStroage().getItem(key)
      try {
        return JSON.parse(value) || null
      } catch (e) {
        return value || null
      }
    }
  },
  remove: function (key) {
    if (isExist(key)) {
      getLocalStroage().removeItem(key)
    }
    return this
  }
}

export function extend() {
  let options
  let name
  let src
  let copy
  let copyIsArray
  let clone
  let target = arguments[0] || {}
  let i = 1
  let length = arguments.length
  let deep = false

  if (typeof target === 'boolean') {
    deep = target
    target = arguments[i] || {}
    i++
  }

  if (typeof target !== 'object' && !isFunction(target)) {
    target = {}
  }

  if (i === length) {
    target = this
    i--
  }

  for (; i < length; i++) {
    if ((options = arguments[i]) != null) {
      for (name in options) {
        src = target[name]
        copy = options[name]

        if (target === copy) {
          continue
        }

        if (deep && copy && (isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) {
          if (copyIsArray) {
            copyIsArray = false
            clone = src && Array.isArray(src) ? src : []
          } else {
            clone = src && isPlainObject(src) ? src : {}
          }
          target[name] = extend(deep, clone, copy)
        } else if (copy !== undefined) {
          target[name] = copy
        }
      }
    }
  }

  return target
}

function isPlainObject(obj) {
  let proto, Ctor

  if (!obj || toString.call(obj) !== '[object Object]') {
    return false
  }

  proto = getProto(obj)

  if (!proto) {
    return true
  }

  Ctor = hasOwn.call(proto, 'constructor') && proto.constructor
  return typeof Ctor === 'function' && fnToString.call(Ctor) === ObjectFunctionString
}

export function isFunction(obj) {
  return typeof obj === 'function' && typeof obj.nodeType !== 'number'
}

function hasOwn() {
  return {}.hasOwnProperty
}

const fnToString = hasOwn.toString

const ObjectFunctionString = fnToString.call(Object)

const getProto = Object.getPrototypeOf

/**
 * 判断是否为整数
 * @param obj
 * @return {boolean}
 */
export function isInteger(obj) {
  return Math.floor(obj) === obj
}

/**
 * 判断是否为数字
 * @param num
 * @return {boolean}
 */
export function isNumber(num) {
  let number = +num

  if ((number - number) !== 0) {
    // Discard Infinity and NaN
    return false
  }

  if (number === num) {
    return true
  }

  if (typeof num === 'string') {
    // String parsed, both a non-empty whitespace string and an empty string
    // will have been coerced to 0. If 0 trim the string and see if its empty.
    if (number === 0 && num.trim() === '') {
      return false
    }
    return true
  }
  return false
}

export const getValueByType = function (list, type, typeName = 'id', valueName = 'value') {
  const target = list.find(item => item[typeName] === type)
  const result = target ? target[valueName] : ''
  return result
} 
